package com.fieldchain.ex.fa100.ui.personal.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.mvp.presenter.LoginPresenter;
import com.fieldchain.ex.fa100.mvp.view.LoginView;
import com.fieldchain.ex.fa100.utils.common.EditTextUtils;
import com.fieldchain.ex.fa100.utils.common.InputAnimation;
import com.fieldchain.ex.fa100.utils.common.VerticalDisUtils;
import com.fieldchain.ex.fa100.utils.net.common.SmsCode;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/3
 */
public class LoginSMSActivity extends MyBaseActivity<LoginPresenter, LoginView> implements LoginView, View.OnClickListener {
    private RelativeLayout reaBack;
    private ImageView ivBac;
    private EditText etPhone;
    private RelativeLayout reaImgcode;
    private ImageView ivImgcode;
    private EditText etImagecode;
    private EditText etSmsver;
    private TextView tvVerSms;
    private RelativeLayout reaDis;
    private TextView tvSure;
    private LinearLayout linBottom;
    private TextView tvSms;
    private TextView tvRegist;


    private EditTextUtils etUtils;
    private InputAnimation animation;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        ivBac = findViewById(R.id.iv_bac);
        etPhone = findViewById(R.id.et_phone);
        reaImgcode = findViewById(R.id.rea_imgcode);
        ivImgcode = findViewById(R.id.iv_imgcode);
        etImagecode = findViewById(R.id.et_imagecode);
        etSmsver = findViewById(R.id.et_smsver);
        tvVerSms = findViewById(R.id.tv_ver_sms);
        reaDis = findViewById(R.id.rea_dis);
        tvSure = findViewById(R.id.tv_sure);
        linBottom = findViewById(R.id.lin_bottom);
        tvSms = findViewById(R.id.tv_sms);
        tvRegist = findViewById(R.id.tv_regist);
    }

    @Override
    public void setValue() {
        reaBack.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        tvVerSms.setOnClickListener(this);
        ivImgcode.setOnClickListener(this);
        tvRegist.setOnClickListener(this);

        etUtils=new EditTextUtils();
        etUtils.append(etSmsver,reaDis);
        etUtils.start();

        setInput();
        presenter.getVer();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_nextlogin;
    }
    public void setInput(){
        animation=new InputAnimation();
        animation.attachView((Activity) mContext,70,etSmsver,linBottom,ivBac);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;

            case R.id.iv_imgcode:
                presenter.getVer();
                break;
            case R.id.tv_sure:
                presenter.login(etPhone.getText().toString(), etSmsver.getText().toString());
                break;

            case R.id.tv_ver_sms:
                VerticalDisUtils.getInstance().start(etSmsver,tvVerSms,reaDis);
                SmsCode.getInstance().getSms(etPhone.getText().toString(),etImagecode.getText().toString());
                break;
            case R.id.tv_regist:
                startActivity(new Intent(mContext, RegistPhoneActivity.class));
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        etUtils.cancle();
        animation.detachView();
        animation=null;
        VerticalDisUtils.getInstance().cancle();
    }

    @Override
    public void loginResult(int type) {
        if(type==200){
            finish();
        }
    }

    @Override
    public void verImage(Bitmap bit) {
        ivImgcode.setImageBitmap(bit);
    }
}
